package com.itcast.jdbctemplate;

import com.itcast.domain.Emp;
import com.itcast.utils.JDBCUtil;
import org.junit.Test;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * JDBC Template 练习
 * Junit 单元测试，让方法独立执行
 */
public class JdbcTemplateDemo2 {

    // 获取 JdbcTemplate 对象
    private JdbcTemplate template = new JdbcTemplate(JDBCUtil.getDataSource());

    /**
     * 修改 1001 号数据的 salary 为 10000
     */
    @Test
    public void test1() {
        // 定义 sql
        String sql = "UPDATE emp SET salary = 10000 WHERE id = ?";
        // 执行
        int count = template.update(sql, 1001);
        System.out.println(count);
    }

    /**
     * 添加一条记录
     */
    @Test
    public void test2() {
        String sql = "INSERT INTO emp (id,ename,dept_id) VALUES (?,?,?)";
        int count = template.update(sql, 1015, "国王", 10);
        System.out.println(count);
    }

    /**
     * 删除上面添加的记录
     */
    @Test
    public void test3() {
        String sql = "DELETE FROM emp WHERE id = ?";
        int count = template.update(sql, 1015);
        System.out.println(count);
    }

    /**
     * 查询 id = 1001 的记录，并封装称 Map
     * 注意：此方法查询的结果集只能有一个。
     */
    @Test
    public void test4() {
        String sql = "SELECT * FROM emp WHERE id = ?";
        Map<String, Object> stringObjectMap = template.queryForMap(sql, 1001);
        System.out.println(stringObjectMap);
    }

    /**
     * 查询所有记录，并封装成 List
     * 将每一条记录封装成一个 Map，将每个 Map 装载到 List 中
     */
    @Test
    public void test5() {
        String sql = "SELECT * FROM emp";
        List<Map<String, Object>> list = template.queryForList(sql);
        for (Map<String, Object> stringObjectMap : list) {
            System.out.println(stringObjectMap);
        }
    }

    /**
     * 查询所有记录，并封装成 List<Emp>
     */
    @Test
    public void test6() {
        String sql = "SELECT * FROM emp";
        List<Emp> list = template.query(sql, new RowMapper<Emp>() {
            @Override
            public Emp mapRow(ResultSet rs, int i) throws SQLException {
                // 获取数据
                int id = rs.getInt("id");
                String ename = rs.getString("ename");
                int jobId = rs.getInt("job_id");
                int mgr = rs.getInt("mgr");
                Date joindate = rs.getDate("joindate");
                double salary = rs.getDouble("salary");
                double bonus = rs.getDouble("bonus");
                int deptId = rs.getInt("dept_id");

                // 创建 Emp 对象，并赋值
                return new Emp(id, ename, jobId, mgr, joindate, salary, bonus, deptId);
            }
        });

        for (Emp emp : list) {
            System.out.println(emp);
        }
    }

    @Test
    public void test6_2() {
        String sql = "SELECT * FROM emp";
        List<Emp> list = template.query(sql, new BeanPropertyRowMapper<Emp>(Emp.class));
        for (Emp emp : list) {
            System.out.println(emp);
        }
    }

    /**
     * 查询总记录数
     */
    @Test
    public void test7() {
        String sql = "SELECT COUNT(id) FROM emp";
        Long aLong = template.queryForObject(sql, Long.class);
        System.out.println(aLong);
    }
}
